#include "display.h"
#include "main.h"
#include "key_control.h"

void print_string(std::string message, int xx, int yy) {
  glColor3f(1.0, 1.0, 1.0);
  glRasterPos2i(xx, yy);
  for (char &c : message)
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}
void print_string_rgb(std::string message, int xx, int yy, float r, float g,
                      float b) {
  glColor3f(r, g, b);
  glRasterPos2i(xx, yy);
  for (char &c : message)
    glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, c);
}

void welcome() {
  glClear(GL_COLOR_BUFFER_BIT);
  glClearColor(0, 0, 0, 1.0);

  print_string("****************", 300, 150);
  print_string("Snake", 370, 181);
  for (int i = 162; i < 220; i += 12) {
    print_string("*", 300, i);
  }
  for (int i = 162; i < 220; i += 12) {
    print_string("*", 492, i);
  }
  print_string("****************", 300, 222);
  int tips_x = 170;
  int tips_y = 300;
  // print_string("To control the snake, ", tips_x, tips_y);
  print_string("Press w or up to go up,  s or down to go down;", tips_x,
               tips_y + 50);
  print_string("Press w or up to go up,  s or down to go down;", tips_x,
               tips_y + 100);
  print_string("Press space to begin or speed up!", tips_x + 80, tips_y + 150);
  show_score();
  glFlush();
}

void display() {
  key_operations();
  if (is_play) {
    tsnake.auto_move();
  } else {
    welcome();
  }
  glFlush();
}

void reshape(int w, int h) {
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  glOrtho(0, 800, 800, 0, -1.0, 1.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
}

void show_score() {
  std::string str = "Score: ";
  glColor3f(0, 0, 0);
  glRecti(72, 10, 150, 35);
  // print_string_rgb(str + std::to_string(last_score), 10, 30, 0, 0, 0);
  print_string(str + std::to_string(score), 10, 30);
  glFlush();
}

void show_speed() {
  std::string str = "Refresh Time: ";
  glColor3f(0, 0, 0);
  glRecti(160, 750, 200, 780);
  // print_string_rgb(str + std::to_string(last_speed_time) + " ms", 10, 770, 0, 0, 0);
  print_string(str + std::to_string(speed_time), 10, 770);
  print_string("ms", 200, 770);

  glFlush();
}